﻿using Ewell.BizComponent.Charge.UI.VM;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace Ewell.BizComponent.Charge.UI
{
    /// <summary>
    /// OnlinePayFrm.xaml 的交互逻辑
    /// </summary>
    public partial class OnlinePayFrm : Window
    {
        public OnlinePayVm vm { get; set; }
        private TaskCompletionSource<bool> continueTcs = null;
        public bool payWaitState { get; set; }
        public OnlinePayFrm()
        {
            InitializeComponent();
            Topmost = true;
        }

        public OnlinePayFrm(OnlinePayVm vm) : this()
        {
            this.vm = vm;
            this.DataContext = this.vm;
        }
        /// <summary>
        /// 支付
        /// </summary>
        internal void PayAll()
        {
            this.Loaded += async (s, e) =>
            {
                await Task.Run(async () =>
                {
                    foreach (var item in this.vm.payWayList)
                    {
                        this.vm.payWay = item;
                        if (this.vm.payWay.hisScanPay && string.IsNullOrEmpty(this.vm.payWay.payQrCode))
                        {
                            bool isContinue=await WaitForContinueAsync();
                            if (!isContinue) break;
                        }
                        this.vm.Pay(item);
                    }
                    
                });
                this.Close();
            };
            this.ShowDialog();
        }
        private async Task<bool> WaitForContinueAsync()
        {
            Dispatcher.Invoke(()=>
            {
                payQrCodeTxt.IsEnabled = true;
                ctnBtn.IsEnabled = true;
                cancelBtn.IsEnabled = true;
            });
            continueTcs = new TaskCompletionSource<bool>();
            return await continueTcs.Task;
        }
        /// <summary>
        /// 继续支付
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PayContinue(object sender, RoutedEventArgs e)
        {
            if (this.vm.payWay.hisScanPay && !string.IsNullOrEmpty(this.vm.payWay.payQrCode))
            {
                payQrCodeTxt.IsEnabled = false;
                ctnBtn.IsEnabled = false;
                cancelBtn.IsEnabled = false;
                continueTcs.SetResult(true);
            }
        }
        /// <summary>
        /// 终止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PayCancel(object sender, RoutedEventArgs e)
        {
            if (this.vm.payWay.hisScanPay)
            {
                payQrCodeTxt.IsEnabled = false;
                ctnBtn.IsEnabled = false;
                cancelBtn.IsEnabled = false;
                continueTcs.SetResult(false);
            }
        }

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.vm.payWay.payQrCode=(sender as TextBox).Text.Trim();
                this.PayContinue(sender, e);
            }
        }
        /// <summary>
        /// 退款
        /// </summary>
        internal void RefundAll()
        {
            this.Loaded += async (s, e) =>
            {
                methodLab.Content = "退款";
                await Task.Run(() =>
                {
                    foreach (var item in this.vm.payWayList)
                    {
                        this.vm.payWay = item;
                        this.vm.Refund(item);
                    }
                });
                this.Close();
            };
            this.ShowDialog();
        }

        
    }
}
